package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.DateUtil;
import org.apache.dubbo.config.annotation.Service;

import java.util.*;

@Service(timeout = 60000)
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {
    @Override
    public boolean importData(List<String[]> data) {
        List<OrderSetting> list = new ArrayList<>();
        for(String[] row : data){
            String orderDate = row[0];
            Integer number = Integer.parseInt(row[1]);
            //判断日期对应的设置是否存在
            OrderSetting orderSetting = findByDate(orderDate);
            if(orderSetting == null){
                orderSetting = new OrderSetting();
            }
            orderSetting.setOrderDate(DateUtil.parseDate(orderDate));
            orderSetting.setNumber(number);
            list.add(orderSetting);
        }
        this.saveOrUpdateBatch(list);
        return true;
    }

    @Override
    public OrderSetting findByDate(String orderDate) {
        QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_date",orderDate);
        return this.getOne(queryWrapper);
    }

    @Override
    public Map selectByYearMonth(int year, int month) {
        Map map = new HashMap();
        QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
        //yyyy-MM
        String yearMonth = year + "-" +(month<10?"0":"") + month;
        queryWrapper.eq("date_format(order_date,'%Y-%m')",yearMonth);
        //select t.*,date_format(order_date,'%Y-%m') from t_order_setting t
        //where date_format(order_date,'%Y-%m') = '2020-11'
        List<OrderSetting> dataList = this.list(queryWrapper);
        //settingData: {//前端展示数据需要的数据格式
        //        '2020-11-01':{number:200,reservations:200},
        //        '2020-11-02':{number:200,reservations:100},
        //        '2020-11-15':{number:200,reservations:0}
        //      },
        for(OrderSetting orderSetting : dataList){
            String orderDate = DateUtil.formatDate(orderSetting.getOrderDate());
            map.put(orderDate,orderSetting);
        }
        return map;
    }

    @Override
    //单个日期的设置
    public boolean settingData(String day, int num) {
        //判断日期对应的设置是否存在
        OrderSetting orderSetting = findByDate(day);
        if(orderSetting == null){
            orderSetting = new OrderSetting();
            orderSetting.setOrderDate(DateUtil.parseDate(day));
        }
        orderSetting.setNumber(num);
        return this.saveOrUpdate(orderSetting);
    }

    @Override
    public boolean deleteOrderSettingByMonth(Date today) {
        Date date = new Date();
        if(date.getTime()>today.getTime()){
            this.baseMapper.deleteOrderSettingByMonth(today);
        }else {
            throw new RuntimeException("该数据无法删除");
        }

        return true;
    }


}
